// This file is part of OpenTSDB.
// Copyright (C) 2015  The OpenTSDB Authors.
// Copyright (C) 2017 TeleDB Development Group
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version.  This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.  You should have received a copy
// of the GNU Lesser General Public License along with this program.  If not,
// see <http://www.gnu.org/licenses/>.
package net.opentsdb.query.filter.value;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Objects;
import com.stumbleupon.async.Deferred;
import net.opentsdb.core.TSDB;
import net.opentsdb.query.filter.TagVFilter;

import java.util.*;

/**
 * A filter that lets the user list one or more explicit strings that should
 * be included in a result set for aggregation. 
 * @since 2.2
 */
public  abstract class TagValueFilterBase extends TagVFilter {

  /** Name of this filter */
//  final public static String FILTER_NAME = "gt";

  /** A list of strings to match on */
  final protected Set<String> literals;

  /** Whether or not the match should be case insensitive */
  final protected boolean case_insensitive;

  /**
   * The default Ctor that disables case insensitivity
   * @param tagk The tag key to associate with this filter
   * @param filter The filter to match on
   * @throws IllegalArgumentException if the tagk or filter were empty or null
   */
  public TagValueFilterBase(final String tagk, final String filter) {
    this(tagk, filter, false);
  }

  /**
   * A ctor that allows enabling case insensitivity
   * @param tagk The tag key to associate with this filter
   * @param filter The filter to match on
   * @param case_insensitive Whether or not to match on case
   * @throws IllegalArgumentException if the tagk or filter were empty or null
   */
  public TagValueFilterBase(final String tagk, final String filter,
                            final boolean case_insensitive) {
    super(tagk, filter);
    this.case_insensitive = case_insensitive;
    
    // we have to have at least one character.
    if (filter == null || filter.isEmpty()) {
      throw new IllegalArgumentException("TsdbFilter cannot be null or empty");
    }
    if (filter.length() == 1 && filter.charAt(0) == '|') {
      throw new IllegalArgumentException("TsdbFilter must contain more than just a pipe");
    }
    final String[] split = filter.split("\\|");
    if (case_insensitive) {
      for (int i = 0; i < split.length; i++) {
        split[i] = split[i].toLowerCase(Locale.ENGLISH);
      }
    }
    literals = new HashSet<String>(Arrays.asList(split));

    filt_value= true;
  }
  
  @Override
  public Deferred<Boolean> match(final Map<String, String> tags) {
      return Deferred.fromResult(false);
  }

  @Override
  public String debugInfo() {
    return "{literals=" + literals + ", case=" + case_insensitive + "}";
  }
  
  /**
   * Overridden here so that we can resolve the literal values if we don't have
   * too many of them AND we're not searching with case insensitivity.
   */
  @Override
  public Deferred<byte[]> resolveTagkName(final TSDB tsdb) {
    return Deferred.fromResult(null);
  }
  
  /** @return Whether or not this filter has case insensitivity enabled */
  @JsonIgnore
  public boolean isCaseInsensitive() {
    return case_insensitive;
  }

  @Override
  public boolean equals(final Object obj) {
    if (obj == null) {
      return false;
    }
    if (!(obj instanceof TagValueFilterBase)) {
      return false;
    }
    if (obj == this) {
      return true;
    }
    final TagValueFilterBase filter = (TagValueFilterBase)obj;
    return Objects.equal(tagk, filter.tagk)
        && Objects.equal(literals, filter.literals)
        && Objects.equal(case_insensitive, filter.case_insensitive);
  }
  
  @Override
  public int hashCode() {
    return Objects.hashCode(tagk, literals, case_insensitive);
  }

//  @Override
//  public String getType() {
//    return FILTER_NAME;
//  }
  
  /** @return a string describing the filter */
  public String description() {
    return "filter tsdb-value great than one value.";
  }
  
  /** @return a list of examples showing how to use the filter */
  public String examples() {
    return "{value=gt(100)}";
  }
}
